package com.xuerhua.yxt.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xuerhua.yxt.common.enums.RelationType;
import com.xuerhua.yxt.dao.AttachmentListMapper;
import com.xuerhua.yxt.dao.TeacherRoutineMapper;
import com.xuerhua.yxt.model.AttachmentList;
import com.xuerhua.yxt.model.TeacherRoutine;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.AttachRelationReqVO;
import com.xuerhua.yxt.reqVo.TeacherRoutineReqVO;
import com.xuerhua.yxt.service.ITeacherPointService;
import com.xuerhua.yxt.service.ITeacherRoutineService;
import com.xuerhua.yxt.service.ITeacherService;
import com.xuerhua.yxt.view.TeacherRoutineView;

@Service("teacherRoutineService")
public class TeacherRoutineServiceImpl implements ITeacherRoutineService {

	@Autowired
	private TeacherRoutineMapper teacherRoutineMapper;
	@Autowired
	private AttachmentListMapper attachmentListMapper;
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private ITeacherPointService teacherPointService;
	
	@Override
	public List<TeacherRoutineView> selectListByPage(Page<TeacherRoutineView> page) {
		return teacherRoutineMapper.selectListByPage(page);
	}

	@Override
	public TeacherRoutineView selectViewById(Integer id) {
		TeacherRoutineView view = teacherRoutineMapper.selectViewById(id);
		if(view.getAttachRelationId() != null) {
			Map<String, Object> map = new HashMap<>();
			map.put("type", RelationType.TEACHER_ROUTINE.getCode());
			map.put("objId", view.getAttachRelationId());
			map.put("schoolId",view.getSchoolId());
			List<AttachmentList> attachmentList = attachmentListMapper.queryAttachmentListByRelationId(map);
			view.setAttachList(attachmentList);
		}
		return view;
	}

	@Override
	public void addTeacherRoutine(TeacherRoutineReqVO vo) {
		if(vo.getAttachIds()!= null) {
			AttachRelationReqVO attachReqVO = new AttachRelationReqVO();
			String[] array = vo.getAttachIds().split(",");
			Long relationId = Long.valueOf(array[0]);
			vo.setAttachRelationId(relationId);
			attachReqVO.setRelationId(relationId);
			attachReqVO.setAttachIds(array);
			attachReqVO.setSchoolId(vo.getSchoolId());
			attachmentListMapper.updateRelationIdByIds(attachReqVO);
		}
		if(vo.getScoreType() == 0) {
			vo.setScore(0 - vo.getScore());
		}
		vo.setJudgeTime(new Date());
		teacherRoutineMapper.addTeacherRoutine(vo);
		new Thread(new TeacherRoutineExecutor(vo)).start();
	}

	@Override
	public int updateByPrimaryKeySelective(TeacherRoutine record) {
		return teacherRoutineMapper.updateByPrimaryKeySelective(record);
	}
	
	class TeacherRoutineExecutor implements Runnable {
			
			private TeacherRoutineReqVO vo;
			
			public TeacherRoutineExecutor(TeacherRoutineReqVO vo) {
				this.vo = vo;
			}
	
			@Override
			public void run() {
				try { 
					String[] array = vo.getTeacherIds();
					for(String teacherId : array) {
						teacherPointService.updateTeacherMonthPoints(
								Integer.valueOf(teacherId), vo.getScore());
					}
		        } catch (Exception e) { 
		            e.printStackTrace(); 
		        }
			}
		}
	
	}
